package com.base.caselib.presenter;

import android.app.Activity;
import android.text.TextUtils;
import com.base.caselib.bean.CheckRecordBean;
import com.base.caselib.bean.TaskInfo;
import com.base.caselib.utils.DownLoadUtils;
import com.base.caselib.view.CheckRecordView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.CasePrint;
import com.base.commonlib.http.CallBackFroPark;
import com.base.commonlib.http.FileBean;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.ResponseT;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.BitmapUtil;
import com.base.commonlib.utils.SystemUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/10/29.
 */

public class CheckRecordPresenter extends BasePresenter<CheckRecordView>{
    //现场检查路段1
    public String fieldInspectionRoad1;
    //现场检查路段2
    public String fieldInspectionRoad2;
    //现场检查参照物1
    public String fieldInspectionReference1;
    //现场检查参照物2
    public String fieldInspectionReference2;
    public CasePrint mcasePrint = new CasePrint();
    public boolean mHasSaved = false;
    public CheckRecordPresenter(CheckRecordView view, Activity activity) {
        super(view, activity);
    }


    public void initPics(String cFormId,String caseId){
        //初始化图片
        mView.showPb_mes("正在初始化数据");
        Map<String,String> params = new HashMap<>();
        params.put("caseId",caseId);
        params.put("formId",cFormId);

        Disposable disposable = RealNetWorkUtil.postForm()
                .method("api-case/punishCase/getfieldRecord")
                .params(params)
                .build().enqueue(CheckRecordBean.class, new CallBackImpl<CheckRecordBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }
                    @Override
                    public void onNext(CheckRecordBean objects) {
                        mView.hidePb_mes();
                        fieldInspectionRoad1 = objects.getFieldInspectionRoad1();
                        fieldInspectionRoad2 = objects.getFieldInspectionRoad2();
                        fieldInspectionReference1 = objects.getFieldInspectionReference1();
                        fieldInspectionReference2 = objects.getFieldInspectionReference2();
                        mView.returnCheckRecordBean(objects);
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    public void upRandom(String caseId,String functionId, String fieldRecord,String investigationDate, List<String> list){
        mView.showPb_mes("正在压缩图片。。。");
        List<String> paths = new ArrayList<>();
        for (String fils : list){
            paths.add(BitmapUtil.compressImage(fils));
        }
        //压缩完成
        mView.hidePb_mes();

        mView.showPb_mes("正在保存数据");
        List<FileBean> mFileList = new ArrayList<>();
        FileBean mFileBean = null;
        for (String str : paths){
            mFileBean = new FileBean("files1",str);
            mFileList.add(mFileBean);
        }

        Disposable enqueue = RealNetWorkUtil.postPics().method("api-case/punishCase/savefieldRecord").filePaths(mFileList)
                .addParams("caseId",caseId)
                .addParams("formId",functionId)
                .addParams("investigationDate",investigationDate)
                .addParams("fieldInspectionRoad1", TextUtils.isEmpty(fieldInspectionRoad1)?"":fieldInspectionRoad1)
                .addParams("fieldInspectionRoad2",TextUtils.isEmpty(fieldInspectionRoad2)?"":fieldInspectionRoad2)
                .addParams("fieldInspectionReference1",TextUtils.isEmpty(fieldInspectionReference1)?"":fieldInspectionReference1)
                .addParams("fieldInspectionReference2",TextUtils.isEmpty(fieldInspectionReference2)?"":fieldInspectionReference2)
                .addParams("fieldRecord",fieldRecord).build().enqueueForUpFiles(new CallBackFroPark<ResponseT>() {
                    @Override
                    public void onResponse(ResponseT responseT) throws Exception {
                        mView.hidePb_mes();
                        mHasSaved = true;
                        mView.showMes(responseT.getData().toString());
                    }

                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                    }

                    @Override
                    public void onNext(ResponseT responseT) {
                        if (responseT == null){
                            mView.hidePb_mes();
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }
    public void getNoticePdfUrl(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("formId","xianchangjiancha");
        params.put("modelId","YPijz0ywa52U8vNioqq");
        params.put("funcIdstart","");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforxcjc")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }


    public void getPdfUrl(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("formId","xianchangjiancha");
        params.put("modelId","y7RgLtwaDqfpJr0j1gq");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforxcjc")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    public void getPdfUrlZLZG(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("formId","xianchangjiancha");
        params.put("modelId","YPijz0ywa52U8vNioqw");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforxcjc")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    public void getPdfUrlAddress(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("formId","xianchangjiancha");
        params.put("modelId","YPijz0ywa52U8vNiomn");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkaz4")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    DownLoadUtils downLoadUtils;
    public void downLoadFiel(String url,String name){
        String[] names = url.split("\\.");
        String fileName = name+"."+names[names.length-1];

        File file = new File(SystemUtil.getAppDir());
        if (!file.exists()){
            file.mkdirs();
        }
        File file1 = new File(SystemUtil.getAppDir()+fileName);
        if (file1.exists()){
            file1.delete();
        }
        TaskInfo info = new TaskInfo();
        info.setName(fileName);
        info.setPath(SystemUtil.getAppDir());
        info.setUrl(url);
        downLoadUtils = new DownLoadUtils(mActivity,info);
        downLoadUtils.showDownDialog();
    }

    @Override
    public void release() {
        super.release();
        if (downLoadUtils != null){
            downLoadUtils.onDestory();
        }
    }
}
